import requests
import json
import urllib3
import pandas as pd
import numpy as np
import socket
import time
from src.facility import *

# for InsecureRequestWarning
urllib3.disable_warnings()

socket.setdefaulttimeout(30)


def back_test_mission_0(delay_day_count):
    csv_path_strategy_lists = 'csv/pool' + '.csv'
    df_strategy_lists = pd.read_csv(csv_path_strategy_lists, encoding='utf-8')
    df_strategy_lists = df_strategy_lists[
        ['unique_id', '策略作者', '策略名字']
    ]

    csv_path_write_lists = []

    for i in range(df_strategy_lists.index.size):
        csv_path_read = 'csv/' + df_strategy_lists.at[i, 'unique_id'] + '.csv'
        df_read = pd.read_csv(csv_path_read, encoding='utf-8')  # 日期递增

        csv_path_write = 'csv/D1_' + df_strategy_lists.at[i, 'unique_id'] + '.csv'
        df_write = pd.DataFrame(columns=['买入日期', '卖出日期', '股票代码', '数量'])

        for j in range(df_read.index.size):
            buy_date = df_read.at[j, '买入日期']
            if pd.isna(buy_date):
                continue
            we_buy_date = get_day(buy_date, delay_day_count)
            add_data = pd.Series({'买入日期': we_buy_date,
                                  '卖出日期': '...',
                                  '股票代码': df_read.at[j, '股票代码'],
                                  '数量': df_read.at[j, '数量']
                                  })
            # print(add_data)
            df_write = df_write.append(add_data, ignore_index=True)
        df_write.to_csv(csv_path_write)
        print('--------%s---------' % (csv_path_write,))
        csv_path_write_lists.append(csv_path_write)
    print(csv_path_write_lists)


def main():
    back_test_mission_0(delay_day_count=1)


if __name__ == '__main__':
    main()